package com.tedu.sp04.controller;

import com.tedu.sp01.entity.Item;
import com.tedu.sp01.entity.Order;
import com.tedu.sp01.entity.User;
import com.tedu.sp01.service.OrderService;
import com.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("/{orderId}")  //http://localhost:8201/123
    public JsonResult<Order> getOreder(@PathVariable String orderId){
        log.info("get order, id="+orderId);
      Order order =   orderService.getOrder(orderId);

        return  JsonResult.ok().data(order);
    }

    @GetMapping("/add")  //get模式下的拼接参数：http://localhost:8201/123?add=1
    public JsonResult<?> add(){
        Order order = new Order();
        order.setId("123abc");
        order.setUser(new User(8,null,null));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1, "商品1", 1),
                new Item(2, "商品2", 2),
                new Item(3, "商品3", 3),
                new Item(4, "商品4", 4),
                new Item(5, "商品5", 5),
                
        }));
        orderService.addOrder(order);
        return JsonResult.ok();
    }
}
